package com.outfoodmanage.controller;

import java.util.List;
import java.util.concurrent.TimeUnit;

import com.outfoodmanage.domain.FoodStore;
import com.outfoodmanage.domain.RechargeRecord;
import com.outfoodmanage.service.IFoodStoreService;
import com.outfoodmanage.service.IRechargeRecordService;
import com.rys.common.utils.Search;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 充值记录Controller
 *
 * @author ruoyi
 * @date 2025-01-23
 */
@Controller
@RequestMapping("/system/record")
public class RechargeRecordController extends BaseController
{
    private String prefix = "system/record";

    @Autowired
    private IRechargeRecordService rechargeRecordService;

    @RequiresPermissions("system:record:view")
    @GetMapping("/{storeId}")
    public String record(@PathVariable("storeId")  String storeId, ModelMap mmap)
    {
        mmap.put("storeId",storeId);
        return prefix + "/record";
    }

    /**
     * 查询充值记录列表
     */
    @RequiresPermissions("system:record:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RechargeRecord rechargeRecord)
    {
        startPage();
        Search search = new Search();
        search.add("storeId",rechargeRecord.getStoreId());
        search.add("status","1");
        List<RechargeRecord> list = rechargeRecordService.getList(search,"id desc");
        return getDataTable(list);
    }


    @RequiresPermissions("system:record:view")
    @GetMapping("/all")
    public String allRecord()
    {
        return prefix + "/allrecord";
    }


    @Resource
    IFoodStoreService foodStoreService;
    @Resource
    RedisTemplate redisTemplate;

    public static String STORENAME = "STOREANMEINFO:";
    /**
     * 查询充值记录列表
     */
    @RequiresPermissions("system:record:list")
    @PostMapping("/alllist")
    @ResponseBody
    public TableDataInfo alllist(RechargeRecord rechargeRecord)
    {
        startPage();
        Search search = new Search();
        search.add("storeId",rechargeRecord.getStoreId());
        search.add("status","1");
        search.add("month",rechargeRecord.getMonth());
        List<RechargeRecord> list = rechargeRecordService.getList(search,"id desc");
        list.forEach(record -> {
            Long storeId = record.getStoreId();
            String storeName = (String) redisTemplate.opsForValue().get(STORENAME+storeId);
            if(storeName == null){
                FoodStore foodStore = foodStoreService.get(record.getStoreId());
                storeName = foodStore.getStoreName();
                redisTemplate.opsForValue().set(STORENAME+record.getStoreId(),storeName,1, TimeUnit.DAYS);
            }
            record.setStoreName(storeName);

        });
        return getDataTable(list);
    }


//    /**
//     * 导出充值记录列表
//     */
//    @RequiresPermissions("system:record:export")
//    @Log(title = "充值记录", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    @ResponseBody
//    public AjaxResult export(RechargeRecord rechargeRecord)
//    {
//        List<RechargeRecord> list = rechargeRecordService.selectRechargeRecordList(rechargeRecord);
//        ExcelUtil<RechargeRecord> util = new ExcelUtil<RechargeRecord>(RechargeRecord.class);
//        return util.exportExcel(list, "record");
//    }
//
//    /**
//     * 新增充值记录
//     */
//    @GetMapping("/add")
//    public String add()
//    {
//        return prefix + "/add";
//    }
//
//    /**
//     * 新增保存充值记录
//     */
//    @RequiresPermissions("system:record:add")
//    @Log(title = "充值记录", businessType = BusinessType.INSERT)
//    @PostMapping("/add")
//    @ResponseBody
//    public AjaxResult addSave(RechargeRecord rechargeRecord)
//    {
//        return toAjax(rechargeRecordService.insertRechargeRecord(rechargeRecord));
//    }
//
//    /**
//     * 修改充值记录
//     */
//    @GetMapping("/edit/{id}")
//    public String edit(@PathVariable("id") Long id, ModelMap mmap)
//    {
//        RechargeRecord rechargeRecord = rechargeRecordService.selectRechargeRecordById(id);
//        mmap.put("rechargeRecord", rechargeRecord);
//        return prefix + "/edit";
//    }
//
//    /**
//     * 修改保存充值记录
//     */
//    @RequiresPermissions("system:record:edit")
//    @Log(title = "充值记录", businessType = BusinessType.UPDATE)
//    @PostMapping("/edit")
//    @ResponseBody
//    public AjaxResult editSave(RechargeRecord rechargeRecord)
//    {
//        return toAjax(rechargeRecordService.updateRechargeRecord(rechargeRecord));
//    }
//
//    /**
//     * 删除充值记录
//     */
//    @RequiresPermissions("system:record:remove")
//    @Log(title = "充值记录", businessType = BusinessType.DELETE)
//    @PostMapping( "/remove")
//    @ResponseBody
//    public AjaxResult remove(String ids)
//    {
//        return toAjax(rechargeRecordService.deleteRechargeRecordByIds(ids));
//    }
}